package com.alvin.abp.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alvin.abp.common.context.CurrentUserContext;

public class TurnoverInterceptor implements HandlerInterceptor {
	
	private String LOGIN_URI = "login.htm";
    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        if(request.getMethod() == "GET" && !request.getRequestURI().contains(LOGIN_URI) && CurrentUserContext.isAnonymous()){
        	if (!response.isCommitted()) {
        		response.sendRedirect(LOGIN_URI);
            } else {
                System.out.println("    Response was already committed!");
            }
        }
        return true;
    }

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
	}
}
